package com.graduation.sports.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.graduation.sports.R;
import com.graduation.sports.base.BaseActivity;
import com.graduation.sports.bean.SportMotionRecord;
import com.graduation.sports.db.DataManager;
import com.graduation.sports.db.RealmHelper;
import com.graduation.sports.util.MySp;
import com.graduation.sports.util.permission.PermissionHelper;
import com.graduation.sports.util.permission.PermissionListener;
import com.graduation.sports.util.permission.Permissions;


import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class  SportsActivity extends BaseActivity {

    @BindView(R.id.image_back)
    ImageView imgBack;
    @BindView(R.id.tv_sport_mile)
    TextView tvSportMile;
    @BindView(R.id.tv_sport_count)
    TextView tvSportCount;
    @BindView(R.id.tv_sport_time)
    TextView tvSportTime;
    @BindView(R.id.btStart)
    Button btStart;

    private DecimalFormat decimalFormat = new DecimalFormat("0.00");

    private final int SPORT = 0x0012;

    private DataManager dataManager = null;
    private String sportType = "";


    @Override
    protected int getLayout() {
        return R.layout.activity_sport;
    }

    @Override
    protected void initDataAndEvent(Bundle savedInstanceState) {
        dataManager = new DataManager(new RealmHelper());

        sportType = getIntent().getExtras().getString("sportType");

        upDateUI();

    }

    @OnClick({R.id.btStart, R.id.image_back})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.btStart) {
            PermissionHelper.requestPermissions(this, Permissions.PERMISSIONS_LOCATION,
                    getResources().getString(R.string.app_name) + "需要获取位置", new PermissionListener() {
                        @Override
                        public void onPassed() {
                            startActivityForResult(new Intent(SportsActivity.this, StartSportActivity.class), SPORT);
                        }
                    });
        } else if (id == R.id.img_back) {
            finish();
        }

    }

    private void upDateUI() {
        if ("跑步".equals(sportType) ) {
        } else {
        }
        try {
            List<SportMotionRecord> records = dataManager.queryRecordList(Integer.parseInt(SPUtils.getInstance().getString(MySp.USERID, "0")));
            if (null != records) {

                double sportMile = 0;
                long sportTime = 0;
                for (SportMotionRecord record : records) {
                    sportMile += record.getDistance();
                    sportTime += record.getDuration();
                }
                tvSportMile.setText(decimalFormat.format(sportMile / 1000d));
                tvSportCount.setText(String.valueOf(records.size()));
                tvSportTime.setText(decimalFormat.format((double) sportTime / 60d));
            }
        } catch (Exception e) {
            LogUtils.e("获取运动数据失败", e);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK)
            return;
        switch (requestCode) {
            case SPORT:
                upDateUI();
                setResult(RESULT_OK);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        if (null != dataManager)
            dataManager.closeRealm();
        super.onDestroy();
    }


}
